package cn.legend.travel.admin.destination.service.impl;

import cn.legend.travel.admin.destination.dao.persist.repository.IDelicacyDetailRepository;
import cn.legend.travel.admin.destination.dao.persist.repository.IDelicacyRepository;
import cn.legend.travel.admin.destination.pojo.param.DelicacyAddNewParam;
import cn.legend.travel.admin.destination.pojo.param.DelicacyUpdateParam;
import cn.legend.travel.admin.destination.pojo.vo.DelicacyAdminVO;
import cn.legend.travel.admin.destination.service.IDelicacyService;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.vo.PageData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class DelicacyServiceImpl implements IDelicacyService {
    @Value("${travel.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    private final IDelicacyRepository delicacyRepository;
    private final IDelicacyDetailRepository delicacyDetailRepository;

    @Override
    public PageData<DelicacyAdminVO> list(int pageNum) {
        return delicacyRepository.list(pageNum, defaultQueryPageSize);
    }

    @Override
    @Transactional
    public void update(DelicacyUpdateParam param) {
        int row = delicacyRepository.update(param);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "服务器忙,请稍后再试");
        }
        row = delicacyDetailRepository.update(param.getId(),param.getContent());
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "服务器忙,请稍后再试");
        }
    }

    @Override
    public void delete(Long id) {
        int row = delicacyRepository.delete(id);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_DELETE, "服务器忙,请稍后再试");
        }
        row = delicacyDetailRepository.deleteByDelicacy(id);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_DELETE, "服务器忙,请稍后再试");
        }
    }

    @Override
    public void addNew(DelicacyAddNewParam param) {
        Long id = delicacyRepository.addNew(param);
        int row = delicacyDetailRepository.addNew(id,param.getContent());
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "服务器忙,请稍后再试");
        }
    }
}
